﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using VXCompany.SilverlightLogging.ServiceProxy;
using System;

namespace VXCompany.SilverlightLogging.MVVM.Models
{
	public static class ExtensionMethods
	{
		/// <summary>
		/// Returns a new Observable collection of type T, containing all the items found in the enumerable.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="enumerable"></param>
		/// <returns></returns>
		public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerable)
		{
			if (enumerable == null)
			{
				return null;
			}
			ObservableCollection<T> oc = new ObservableCollection<T>();
			foreach (var item in enumerable)
			{
				oc.Add(item);
			}
			return oc;
		}

		/// <summary>
		/// Excecutes Action on every item in enumerable.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="enumerable"></param>
		/// <param name="action"></param>
		public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
		{
			if (enumerable == null)
			{
				return;
			}

			foreach (var item in enumerable)
			{
				action(item);
			}
		}
	}
}
